package com.v2future.sjt.collegecheckingattendance.utils;

import android.annotation.SuppressLint;
import android.text.TextUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

	/**
	 * 获得指定日期的前一天
	 * 
	 * @param specifiedDay
	 * @return
	 * @throws Exception
	 */
	public static String getSpecifiedDayBefore(String specifiedDay) {// 可以用new
																		// Date().toLocalString()传递参数
		Calendar c = Calendar.getInstance();
		Date date = null;
		try {
			date = new SimpleDateFormat("yyyy-MM-dd").parse(specifiedDay);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		c.setTime(date);
		int day = c.get(Calendar.DATE);
		c.set(Calendar.DATE, day - 1);

		String dayBefore = new SimpleDateFormat("yyyy-MM-dd").format(c
				.getTime());
		return dayBefore;
	}

	/**
	 * 获得指定日期的后一天
	 * 
	 * @param specifiedDay
	 * @return
	 */
	public static String getSpecifiedDayAfter(String specifiedDay) {
		Calendar c = Calendar.getInstance();
		Date date = null;
		try {
			date = new SimpleDateFormat("yyyy-MM-dd").parse(specifiedDay);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		c.setTime(date);
		int day = c.get(Calendar.DATE);
		c.set(Calendar.DATE, day + 1);

		String dayAfter = new SimpleDateFormat("yyyy-MM-dd")
				.format(c.getTime());
		return dayAfter;
	}

	/**
	 * 指定日期的后x天
	 * 
	 * @date 2015年6月29日 上午9:55:18
	 * @author shijianting
	 * @param specifiedDay
	 * @param x
	 * @return
	 */
	public static String getSpecifiedDayAfterX(String specifiedDay, int x) {
		Calendar c = Calendar.getInstance();
		Date date = null;
		try {
			date = new SimpleDateFormat("yyyy-MM-dd").parse(specifiedDay);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		c.setTime(date);
		int day = c.get(Calendar.DATE);
		c.set(Calendar.DATE, day + x);

		String dayAfter = new SimpleDateFormat("yyyy-MM-dd")
				.format(c.getTime());
		return dayAfter;
	}

	/**
	 * 指定日期的前x天
	 * 
	 * @date 2015年7月2日 上午11:08:58
	 * @author shijianting
	 * @param specifiedDay
	 * @param x
	 * @return
	 */
	public static String getSpecifiedDayBeforeX(String specifiedDay, int x) {
		Calendar c = Calendar.getInstance();
		Date date = null;
		try {
			date = new SimpleDateFormat("yyyy-MM-dd").parse(specifiedDay);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		c.setTime(date);
		int day = c.get(Calendar.DATE);
		c.set(Calendar.DATE, day - x);

		String dayAfter = new SimpleDateFormat("yyyy-MM-dd")
				.format(c.getTime());
		return dayAfter;
	}

	public final static int INRANGE = 101;// 在时间范围内，不等于首末
	public final static int EQSTART = 102;// 等于开始时间
	public final static int EQEND = 103;// 等于结束时间
	public final static int OUTRANGE = 104;// 不在范围内
	public final static int EQEACH = 105;// 既等于开始时间也等于结束时间

	/**
	 * 判断日期是否在范围内
	 * 
	 * @param currentDate
	 *            当前日期
	 * @param startDate
	 *            开始日期
	 * @param endDate
	 *            结束日期
	 * @return
	 */
	public static int isInDateRange(String currentDate, String startDate,
									String endDate) {
		if (!TextUtils.isEmpty(currentDate) && !TextUtils.isEmpty(startDate)
				&& !TextUtils.isEmpty(endDate)) {

			int startCompare = currentDate.compareTo(startDate);
			int endCompare = currentDate.compareTo(endDate);

			if (startCompare > 0 && endCompare < 0) {
				return INRANGE;

			} else if (startCompare == 0) {
				if (endCompare == 0) {
					return EQEACH;
				}
				return EQSTART;
			} else if (endCompare == 0) {
				return EQEND;
			}

		}
		return OUTRANGE;

	}

	/**
	 * 获取当前日期是星期几
	 * 
	 * @param dt
	 * @return 当前日期是星期几
	 */
	public static String getWeekOfDate(String dt) {
		Date date = null;
		try {
			date = new SimpleDateFormat("yyyy-MM-dd").parse(dt);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		String[] weekDays = { "周日", "周一", "周二", "周三", "周四", "周五", "周六" };
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if (w < 0)
			w = 0;
		return weekDays[w];
	}

	/**
	 * 日期类型，String转Date
	 * 
	 * @param dt
	 * @return
	 */
	public static Date stringToDate(String dt) {
		Date date = null;
		try {
			date = new SimpleDateFormat("yyyy-MM-dd").parse(dt);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * 日期类型，Date转String
	 * 
	 * @param dt
	 * @return
	 */
	public static String dateToString(Date dt) {
		String date = "";
		try {
			date = new SimpleDateFormat("yyyy-MM-dd").format(dt);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * 获取年日 int型
	 * 
	 * @return
	 */
	public static int[] getYMD(String date) {
		int[] ymd = new int[3];
		try {
			String[] temp = date.split("-");
			for (int i = 0; i < temp.length; i++) {
				ymd[i] = Integer.parseInt(temp[i]);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		return ymd;
	}

	/**
	 * 本地历史记录存储时间
	 * 
	 * @param dt
	 * @return
	 */
	public static String dateToStringForHistory(Date dt) {
		String date = "";
		try {
			date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(dt);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * 将时间字符串转化成date类型
	 * 
	 * @param currentDate
	 * @return
	 */
	public static Date stringToDateForHistory(String currentDate) {
		Date date = null;
		try {
			date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
					.parse(currentDate);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * string转calendar
	 * 
	 * @param currentDate
	 * @return
	 */
	public static Calendar stringToCalendar(String currentDate) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Date date = sdf.parse(currentDate);
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			return calendar;
		} catch (Exception e) {
			// TODO: handle exception
			return null;
		}
	}

	@SuppressLint("SimpleDateFormat")
	public static String getCurrentDateTime(){
		// 初始化日期我当前日期
		String dateTime = "";
		try {
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			dateTime = df.format(new Date());
			return dateTime;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return dateTime;
	}

	@SuppressLint("SimpleDateFormat")
	public static String getCurrentDateTimeOnleyNumber(){
		// 初始化日期我当前日期
		String dateTime = "";
		try {
			SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
			dateTime = df.format(new Date());
			return dateTime;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return dateTime;
	}

	/**
	 * 获取月份
	 * @return
     */
	public static String getCurrentDateMonth(){
		// 初始化日期我当前日期
		String dateTime = "";
		try {
			SimpleDateFormat df = new SimpleDateFormat("MM");
			dateTime = df.format(new Date());
			return dateTime;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return dateTime;
	}

	public static String getCurrentDateYMD(){
		// 初始化日期我当前日期
		String dateTime = "";
		try {
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			dateTime = df.format(new Date());
			return dateTime;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return dateTime;
	}

	public static String getCurrentDateTimeMS(){
		// 初始化日期我当前日期
		String dateTime = "";
		try {
			SimpleDateFormat df = new SimpleDateFormat("mm:ss");
			dateTime = df.format(new Date());
			return dateTime;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return dateTime;
	}
}
